package com.lg.erp.controller.zhanghuan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.zhanghuan.TLogistics;
import com.lg.erp.service.zhanghuan.TLogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/***
 *
 * 物流公司
 * */
@RestController
public class TLogisticsController {
    @Autowired
    private TLogisticsService TLogisticsService;

    private static User user;

    //分页
    @RequestMapping(value = "/logistics", method = RequestMethod.GET)
    public JsonResult PageLogistics(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,String logisticsName) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof User){
             user = (User) principal;
            System.out.println("用户名："+user.getUsername());
        }

        logisticsName = "%" + logisticsName + "%";
        Page<TLogistics> page = new Page<>(current, 5);
        IPage<TLogistics> logisticsIPage = TLogisticsService.pageLogistics(page,logisticsName);
        return new JsonResult(1, "ok", logisticsIPage);
    }

    @PostMapping("/logistic")
    public JsonResult addLogistics(@RequestBody TLogistics logistic) {
        String id = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
        LocalDateTime dateTime = LocalDateTime.now();
        logistic.setLogisticsId(id);
        logistic.setJoinTime(dateTime);//加入时间
        logistic.setCreateTime(dateTime);//创建时间
        TLogisticsService.save(logistic);
        return new JsonResult(1, "ok", null);
    }

    @PutMapping("/logistic")
    public JsonResult updateLogistics(@RequestBody TLogistics logistic) {
        LocalDateTime dateTime = LocalDateTime.now();
        logistic.setUpdateTime(dateTime);
//        QueryWrapper<TLogistics> wrapper = new QueryWrapper<>();
//        wrapper.eq("logistics_id", logistic.getLogisticsId());
//        TLogisticsService.update(logistic, wrapper);
        TLogisticsService.update(logistic, new QueryWrapper<TLogistics>().eq("logistics_id", logistic.getLogisticsId()));
        return new JsonResult(1, "ok", null);
    }

    @DeleteMapping("/logistic/{id}")
    public JsonResult deleteLogistics(@PathVariable String id) {
        System.out.println("删除：" + id);
        TLogisticsService.remove(new QueryWrapper<TLogistics>().eq("logistics_id", id));
        return new JsonResult(1, "ok", null);
    }





    }
